package com.dogstalks.join;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.dogstalks.date.R;
import com.dogstalks.main.BalloonItemizedOverlay;
import com.dogstalks.main.BalloonOverlayView;
import com.dogstalks.util.GPSManager;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MoveMarkerItemizedOverlay extends ItemizedOverlay<OverlayItem> {
	public static final String DEBUG = "dogstalks";
	private Context mContext;
	private MapView mMapView;
	private final MapController mc;
	private List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private Drawable marker = null;
	private OverlayItem inDrag = null;
	private ImageView dragImage = null;
	private int xDragImageOffset = 0;
	private int yDragImageOffset = 0;
	private int xDragTouchOffset = 0;
	private int yDragTouchOffset = 0;

	private BalloonOverlayView mBalloonView;
	private View mClickView;
	private int viewOffset;

	public MoveMarkerItemizedOverlay(Drawable marker, MapView mapview,
			ImageView markerImage) {
		super(boundCenter(marker));
		// TODO Auto-generated constructor stub
		this.mContext = mapview.getContext();
		this.mMapView = mapview;
		this.marker = marker;
		this.dragImage = markerImage;
		// dragImage = (ImageView)this.context.findViewById(R.id.dragImg);
		xDragImageOffset = dragImage.getDrawable().getIntrinsicWidth() / 2;
		yDragImageOffset = dragImage.getDrawable().getIntrinsicHeight();
		mc = mMapView.getController();

		dragImage.setVisibility(View.GONE);
		populate();
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return mOverlays.size();
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		// TODO Auto-generated method stub
		super.draw(canvas, mapView, shadow);

	}

	@Override
	protected OverlayItem createItem(int i) {
		// TODO Auto-generated method stub
		return mOverlays.get(i);
	}

	public void addOverlay(OverlayItem overlay) {
		mOverlays.add(overlay);
		populate();
	}
	public GeoPoint getPoint() {
		return mOverlays.get(0).getPoint();
	}
	
	public void removeItem() {
		mOverlays.clear();
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
		// TODO Auto-generated method stub
		final int action = event.getAction();
		final int x = (int) event.getX();
		final int y = (int) event.getY();
		boolean result = false;

		if (action == MotionEvent.ACTION_DOWN) {
			for (OverlayItem item : mOverlays) {
				Point p = new Point(0, 0);
				hideBalloon();

				mapView.getProjection().toPixels(item.getPoint(), p);

				if (hitTest(item, marker, x - p.x, y - p.y)) {
					result = true;
					inDrag = item;
					mOverlays.remove(inDrag);
					populate();

					xDragTouchOffset = 0;
					yDragTouchOffset = 0;

					setDragImagePosition(p.x, p.y);
					dragImage.setVisibility(View.VISIBLE);

					xDragTouchOffset = x - p.x;
					yDragTouchOffset = y - p.y;

					break;
				}
			}
		} else if (action == MotionEvent.ACTION_MOVE && inDrag != null) {
			setDragImagePosition(x, y);
			result = true;
		} else if (action == MotionEvent.ACTION_UP && inDrag != null) {
			dragImage.setVisibility(View.GONE);

			GeoPoint pt = mapView.getProjection().fromPixels(
					x - xDragTouchOffset, y - yDragTouchOffset);
			GPSManager gpsManager = new GPSManager(mContext, mc);
			String addr = gpsManager.findAddress(pt);
			OverlayItem toDrop = new OverlayItem(pt, addr, inDrag.getSnippet());

			mOverlays.add(toDrop);
			populate();

			showBallonn(addr);
			inDrag = null;

			result = true;
		}

		return (result || super.onTouchEvent(event, mapView));
	}

	private void setDragImagePosition(int x, int y) {
		RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) dragImage
				.getLayoutParams();

		lp.setMargins(x - xDragImageOffset - xDragTouchOffset, y
				- yDragImageOffset - yDragTouchOffset, 0, 0);
		dragImage.setLayoutParams(lp);
	}

	public void removeOverlay() {
		if (mOverlays.size() > 0)
			mOverlays.remove(0);
		mMapView.getOverlays().remove(mOverlays);
		if (mOverlays != null) {
			populate();
		}
	}

	public void showBallonn(String address) {
		Log.d(DEBUG, "showBallonn()");
		if (mMapView != null) {
			boolean isRecycled;
			final int curIndex;
			GeoPoint point;
			int index = 0;
			// curIndex = index;
			point = createItem(index).getPoint();

			if (mBalloonView == null) {
				mBalloonView = new BalloonOverlayView(mMapView.getContext(),
						viewOffset, "address");
				mClickView = (View) mBalloonView.findViewById(R.id.innerLayout);
				isRecycled = false;
			} else {
				isRecycled = true;
			}

			mBalloonView.setVisibility(View.GONE);
			mBalloonView.setData(createItem(index));
			// mBalloonView.setData(createItem(index));

			MapView.LayoutParams params = new MapView.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
					point, MapView.LayoutParams.BOTTOM_CENTER);
			params.mode = MapView.LayoutParams.MODE_MAP;

			mBalloonView.setVisibility(View.VISIBLE);

			if (isRecycled) {
				mBalloonView.setLayoutParams(params);
			} else {
				mMapView.addView(mBalloonView, params);
			}

			mc.animateTo(point);
		}
	}

	private void hideBalloon() {
		if (mBalloonView != null) {
			mBalloonView.setVisibility(View.GONE);
		}
	}

}
